const App = {
    data() {
        return {
            photo,
            Tabletdata: [],
            ScheduledTasks:[{
                Scheduled_Name: '获取第三方系统token任务',
                TaskName: 'partyThreeAppLogicImpl',
                Method: 'refreshAccessTokenForWeChatMiniProgram',
                Params: '0',
                Cron: '*/7000 * * * * ?',
                timer: "7000",
                time: 's',
                type: '2',
                istrue: false,
            },{
                Scheduled_Name:'队列提交任务',
                TaskName:'generalBusinessLogicImpl',
                Method:'generalSaveByRedis',
                Params:'0',
                Cron:'*/1 * * * * ?',
                timer:"1",
                time:'s',
                type:'2',
                istrue:false,
            },{
                Scheduled_Name:'队列提交任务',
                TaskName:'sendMessageByMiniProgramImpl',
                Method:'sendGridTaskMessage',
                Params:'1',
                Cron:'0 0 9 * * ?',
                timer:"09:00",
                time:'s',
                type:'1',
                istrue:false,
            },{
                Scheduled_Name:'队列提交任务',
                TaskName:'sendMessageByMiniProgramImpl',
                Method:'sendPatrolTaskMessage',
                Params:'1',
                Cron:'0 0 9 * * ?',
                timer:"09:00",
                time:'s',
                type:'1',
                istrue:false,
            }],
            page: {
                value1:'',
                PageNumber: 1, //页码
                RowAmount: 10,
            },
        }
    },
    mounted() {
        this.Tabletdata = this.ScheduledTasks;
        this.TimerTask();
    },
    methods: {
        check() {
            this.Tabletdata = this.ScheduledTasks.filter(i=>i.Scheduled_Name.includes(this.page.value1))
        },
        reset() {
            window.location.reload();
        },
        handleSizeChange(newSize){
            this.page.PageNumber = 1;
            this.page.RowAmount = newSize;
        },
        handleCurrentChange(newPage){
            this.page.PageNumber = newPage;
        },
        TimerTask(){
            const that = this;
            $.ajax({
                headers: requestHeaders(),
                url: `${mh}/TimerTask/TimerTaskList`,
                type: 'GET',
                success: function (data) {
                    that.ScheduledTasks.forEach(item =>{
                        let is = true;
                        for (let key in data) {
                            if ((item.TaskName + item.Method) == key) {
                                item.istrue = true;
                                is = false;
                                break;
                            }
                        }
                        if (is) {
                            item.istrue = false;   
                        }
                    })
                },
                complete: function (XMLHttpRequest) {
                    requestPretreatment(XMLHttpRequest);
                },
            })   
        },
        Scheduled(row) {
            const that = this
            if (!row.istrue) {//开启定时器
                if (row.timer == null) {
                    this.$message({
                        message: '请选择执行时间',
                        type: 'warning'
                    })
                    return
                }
                if (row.type == '1') {//每日指定日期执行
                    let timer = row.timer.split(":")[0]
                    if (timer.charAt(0) == '0') {
                        timer = timer.charAt(1);
                    }
                    row.Cron = `0 0 ${timer} * * ?`
                } else if (row.type == '2') {//按时间区域
                    if (row.time == 's') {
                        row.Cron = `*/${row.timer} * * * * ?`
                    } else if (row.time == 'm') {
                        row.Cron = `0 */${row.timer} * * * ?`
                    } else if (row.time == 'h') {
                        row.Cron = `0 0 */${row.timer} * * ?`
                    }
                }
                $.ajax({
                    headers: requestHeaders(),
                    url: `${mh}/TimerTask/exectueTimerTask`,
                    type: 'GET',
                    data: {
                        TaskName: row.TaskName,
                        Method: row.Method,
                        Params: row.Params,
                        Cron: row.Cron
                    },
                    complete: function (XMLHttpRequest) {
                        if (XMLHttpRequest.status == 200) {
                            that.$message.success("已开启");
                            that.TimerTask();
                        } else {
                            that.$message.error("系统繁忙");
                            requestPretreatment(XMLHttpRequest);
                        }
                    }
                })
            } else {//关闭定时器
                $.ajax({
                    headers: requestHeaders(),
                    url: `${mh}/TimerTask/cancelTimerTask`,
                    type: 'GET',
                    data: {
                        TaskName: row.TaskName,
                        Method: row.Method,
                        Params: row.Params,
                    },
                    complete: function (XMLHttpRequest) {
                        if (XMLHttpRequest.status == 200) {
                            that.$message.success("已关闭");
                            that.TimerTask();
                        } else {
                            that.$message.error("系统繁忙");
                            requestPretreatment(XMLHttpRequest);
                        }
                    }
                })
            }
        }
    },
};

const { createApp } = Vue;
const app = createApp(App);
//这种方式是引入全部图标，无需单个引入
for (const [key, component] of Object.entries(ElementPlusIconsVue)) {
    app.component(key, component);
}
const ElMessage = ElementPlus.ElMessage;
app.use(ElementPlus, { locale: zhCn });
app.mount("#app");
